package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Role;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.RowId;
import java.util.List;
@Service
@Slf4j
public class RoleServicelmpl implements RoleService {

    @Autowired
    RoleDao roleDao;
    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public List<Integer> findRoleIdsByUserId(Integer userId) {

        return roleDao.findRolesByUserId1(userId);
    }



    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        log.debug("service:" + queryPageBean);
        log.debug("RoleServiceImpl层的findByPage方法被访问了");
        //1. 开始分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        //2. 条件查询
        Page<Role> page = roleDao.findByCondition(queryPageBean.getQueryString());
        log.debug("RoleServiceImp层的 findByPage方法成成功");
        return  new PageResult(page.getTotal(), page);
    }




    @Override
    public void del(int id) {

        try {
            roleDao.del(id);
        } catch (Exception e) {
            throw new BusinessRuntimeException("该角色被关联，不能删除！！");
        }
        roleDao.delRoleIdAndMenuId(id);
        roleDao.delRoleIdAndPermissionId(id);
    }

    @Override
    public void edit(Role role) {

    }

    @Override
    public Role findRoleById(Integer id) {
        log.debug("RoleServiceImpl层的findRoleById方法被访问了");
        Role roleById = roleDao.findRoleById(id);
        log.debug("RoleServiceImpl层的findRoleById方法被访问了");
        return roleById;
    }

    @Override
    //更新角色
    public void editRole(Role role) {
        roleDao.editRole(role);
    }

    @Override
    //更新角色权限中间表
    public void editRoleIdAndPermissionId(Integer rid, Integer[] permissionIds) {
        //根据角色id删除对应的中间表中的数据
        roleDao.delRoleIdAndPermissionId(rid);
        //遍历添加
        for (Integer permissionId : permissionIds) {
            //根据角色id添加对应的中间表中的数据
            roleDao.editRoleIdAndPermissionId(rid,permissionId);
        }

    }

    @Override
    ////更新角色菜单中间表
    public void editRoleIdAndMenuId(Integer rid, Integer[] menusIds) {
        roleDao.delRoleIdAndMenuId(rid);

        for (Integer menusId : menusIds) {
            roleDao.editRoleIdAndMenuId(rid,menusId);
        }


    }

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menusIds) {
         roleDao.add(role);
        Integer rid = role.getId();
        log.debug("-----------------添加成功后返回生成的id"+rid);
        log.debug("添加角色成功,准备添加对应的中间表信息");
        for (Integer permissionId : permissionIds) {
            roleDao.editRoleIdAndPermissionId(rid,permissionId);
        }
        log.debug("添加角色权限中间表信息成功,准备添加对应的角色菜单中间表信息");
        for (Integer menusId : menusIds) {
            roleDao.editRoleIdAndMenuId(rid,menusId);
        }
        log.debug("角色菜单中间表信息添加成功");

    }
}
